package site.enjoyrun.controller;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import site.enjoyrun.model.Order;
import site.enjoyrun.service.OrderService;
import site.enjoyrun.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @GetMapping("/")
    public Result getOrder(){
        Result res = new Result();

        List<Order> orders = orderService.getAllOrder();

        if(orders == null) {
            res.setMsg("数据获取成功");
            res.setCode(10500);
        } else {
            res.setMsg("数据获取成功");
            res.setCode(10200);
        }

        res.setData(orders);
        return res;
    }

    @PostMapping("/insert")
    public Result insert(Order order) {
        Result res = new Result();
        if(orderService.insertOrder(order)) {
            res.setCode(10200);
            res.setMsg("数据插入成功");
            res.setData(new Boolean(true));
        } else {
            res.setCode(10500);
            res.setMsg("数据异常或者服务器出错");
            res.setData(new Boolean(false));
        }

        return res;
    }

    @PutMapping("/update")
    public Result updateOrder(Order order) {
        Result res = new Result();

        if(orderService.updateOrder(order)) {
            res.setCode(10200);
            res.setMsg("数据修改成功");
            res.setData(new Boolean(true));
        } else {
            res.setCode(10500);
            res.setMsg("数据异常或者服务器出错");
            res.setData(new Boolean(false));
        }

        return res;
    }

    @DeleteMapping("/delete/{oId}")
    public Result deleteOrder(@PathVariable("oId") String oId) {
        Result res = new Result();

        if(orderService.deleteOrder(oId)) {
            res.setCode(10200);
            res.setMsg("数据删除成功");
            res.setData(new Boolean(true));
        } else {
            res.setCode(10500);
            res.setMsg("数据异常或者服务器出错");
            res.setData(new Boolean(false));
        }

        return res;
    }
}
